import qs from 'qs';

function _getUrlParams(link) {
	return qs.parse(link.split('?')[1]);
}

// 微信公众号授权
function wxAuthorize() {
    let link = window.location.href;
    let params = _getUrlParams(link);  // 地址解析

       // 已经授权登录过的就不用再授权了
    // if (store.state.token) return;

    // 如果拿到code，调用授权接口，没有拿到就跳转微信授权链接获取
    if (params.code) {
        api.wxAuth(params.code); // 调用后台接口，授权
    } else {
        let appid = 'wxcb8ae2656305c1e2';
        let uri = encodeURIComponent(link);
        let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=3&connect_redirect=1#wechat_redirect`;
        window.location.href = authURL;
		// alert(link)
    }
	
}



 export {
        wxAuthorize
    }